﻿/*
 * Programer: JR Padfield
 * Description: Create Character Screen. Handles creating a new Chracter.
 * Version: 1
 * Date: 12/17/13 * 
 */

using System;
using System.Collections.Generic;
using TheWorld.Components;
using TheWorldLibrary.Characters;
using TheWorldLibrary.Skills;
using TheWorld.Global;
using TheWorld.Networking;
using TheWorldLibrary.GameStates;
using TheWorldLibrary.Sprites;
using TheWorldLibrary.Controls;
using Microsoft.Xna.Framework;

namespace TheWorld.GameScreens
{
    public class CreateCharacterScreen : BaseGameState
    {
        #region Field Region

        int gender;
        string className;
        TextBox txtName;

        Button cancel, register;

        PictureBox characterImage;
        LinkLabel selectSprite, selectSex, selectClass;

        string[] genderItems = { "Male", "Female" };
        string[] classItems;

        Entity entity;

        #endregion

        #region Property Region

        #endregion

        #region Constructor Region

        public CreateCharacterScreen(Game game, GameStateManager stateManager)
            : base(game, stateManager)
        {
        }

        #endregion

        #region XNA Method Region

        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();

            classItems = new string[DataManager.EntityData.Count];

            int counter = 0;

            foreach (string className in DataManager.EntityData.Keys)
            {
                classItems[counter] = className;
                counter++;
            }

            CreateControls();
        }

        public override void Update(GameTime gameTime)
        {
            ControlManager.Update(gameTime, PlayerIndex.One);

            if (txtName.IsActive && txtName.Text == "Click here to set your name.")
                txtName.Text = "";

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();

            base.Draw(gameTime);

            ControlManager.Draw(GameRef.SpriteBatch);

            GameRef.SpriteBatch.End();
        }

        #endregion

        #region Method Region

        private void CreateControls()
        {
            selectClass = new LinkLabel();
            selectClass.Text = "[Select Class]";
            selectClass.SpriteFont = GfxManager.Instance.FONT_TEXTURE[1];
            selectClass.Position = new Vector2(512 - General.Instance.getStringWidth(1, selectClass.Text) / 2, 400);
            selectClass.Selected += new EventHandler(selectClass_Selected);
            ControlManager.Add(selectClass);

            selectSex = new LinkLabel();
            selectSex.Text = "[Select Sex]";
            selectSex.SpriteFont = GfxManager.Instance.FONT_TEXTURE[1];
            selectSex.Position = new Vector2(512 - General.Instance.getStringWidth(1, selectSex.Text) / 2, 420);
            selectSex.Selected += new EventHandler(selectSex_Selected);
            ControlManager.Add(selectSex);

            selectSprite = new LinkLabel();
            selectSprite.Text = "[Select Sprite]";
            selectSprite.SpriteFont = GfxManager.Instance.FONT_TEXTURE[1];
            selectSprite.Position = new Vector2(512 - General.Instance.getStringWidth(1, selectSprite.Text) / 2, 440);
            selectSprite.Selected += new EventHandler(selectSprite_Selected);
            ControlManager.Add(selectSprite);

            characterImage = new PictureBox(GfxManager.Instance.Sprites[Static.rSprite], new Rectangle((int)selectClass.Position.X + 45, 350, 32, 32), new Rectangle(0, 0, 32, 32));
            ControlManager.Add(characterImage);

            register = new Button(GfxManager.Instance.Buttons[16], GfxManager.Instance.Buttons_H[16], GfxManager.Instance.Buttons_C[16], new Vector2(363, 500), true);
            register.Clicked += new EventHandler(btnRegister_Clicked);
            ControlManager.Add(register);

            cancel = new Button(GfxManager.Instance.Buttons[6], GfxManager.Instance.Buttons_H[6], GfxManager.Instance.Buttons_C[6], new Vector2(520, 500), true);
            cancel.Clicked += new EventHandler(btnBack_Clicked);
            ControlManager.Add(cancel);

            txtName = new TextBox(GameRef, GfxManager.Instance.FONT_TEXTURE[1], GfxManager.Instance.Gui[3], GfxManager.Instance.Gui[13], GameRef.SpriteBatch);
            txtName.Position = new Vector2(512 - 128, 315);
            txtName.Text = "Click here to set your name.";
            Components.Add(txtName);
        }

        void selectClass_Selected(object sender, EventArgs e)
        {
            className = "Fighter";
            selectClass.Text = "Class: Fighter";
        }

        private void CreatePlayer()
        {
            Dictionary<AnimationKey, Animation> animations = new Dictionary<AnimationKey, Animation>();

            Animation animation = new Animation(3, 32, 32, 0, 0);
            animations.Add(AnimationKey.Down, animation);

            animation = new Animation(3, 32, 32, 0, 32);
            animations.Add(AnimationKey.Left, animation);

            animation = new Animation(3, 32, 32, 0, 64);
            animations.Add(AnimationKey.Right, animation);

            animation = new Animation(3, 32, 32, 0, 96);
            animations.Add(AnimationKey.Up, animation);

            AnimatedSprite sprite = new AnimatedSprite(
                characterImage.Image,
                animations);

            EntityGender genders;
            if (gender == 1)
            {
                genders = EntityGender.Male;
            }
            else
            {
                genders = EntityGender.Female;
            }
            
            entity = new Entity(
                txtName.Text,
                Static.rSprite,
                DataManager.EntityData["Fighter"],
                genders,
                EntityType.Character);

            foreach (string s in DataManager.SkillData.Keys)
            {
                Skill skill = Skill.FromSkillData(DataManager.SkillData[s]);
                entity.Skills.Add(s, skill);
            }

            Character character = new Character(entity, sprite);

            GamePlayScreen.Player = new Player(GameRef, character);

            Mapping.Instance.Worlds.CurrentLevel = 0;
            
        }

        void selectSprite_Selected(object sender, EventArgs e)
        {
            General.Instance.changeSprite();
        }

        void btnRegister_Clicked(object sender, EventArgs e)
        {
            CreatePlayer();
            ClientTcp.Instance.SendCreateCharacter(Static.AccountName, entity, Static.rSprite);            
        }

        void btnBack_Clicked(object sender, EventArgs e)
        {
            Transition(ChangeType.Push, Client.MainMenuScreen);
        }

        void selectSex_Selected(object sender, EventArgs e)
        {
            if (gender == 1)
            {
                gender = 0;
                selectSex.Text = "Gender: Male";
            }
            else
            {
                gender = 1;
                selectSex.Text = "Gender: Female";
            }
        }

        #endregion
    }
}
